//
//  JFNoteViewController+CollectionView.m
//  JobFree2.0
//
//  Created by xxcao on 2017/5/26.
//  Copyright © 2017年 Dale. All rights reserved.
//

#import "JFNoteViewController+CollectionView.h"
#import "SwipeView.h"
#import "JFNoteCell.h"
#import "JFNoteHeader.h"
#import "MJRefresh.h"
#import "YYKit.h"
#import "NSObject+FBKVOController.h"

static NSString *CellIdentify = @"CollectionViewCellIdentify";
static NSString *HeadIdentify = @"CollectionViewHeadIdentify";

@implementation JFNoteViewController (CollectionView)

- (UICollectionView *)setupCollectonView {
    UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.swipeView.bounds collectionViewLayout:collectionViewLayout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.delaysContentTouches = NO;
    collectionView.allowsMultipleSelection = NO;
    [collectionView registerNib:[UINib nibWithNibName:@"JFNoteCell" bundle:nil] forCellWithReuseIdentifier:CellIdentify];
    [collectionView registerNib:[UINib nibWithNibName:@"JFNoteHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeadIdentify];
    collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //refresh code
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [collectionView.mj_header endRefreshing];
        });
    }];
    
    UIView *view = [[UIView alloc] initWithFrame:collectionView.bounds];
    view.backgroundColor = [UIColor whiteColor];
    collectionView.backgroundView = view;

    UIImageView *lineImgV = [[UIImageView alloc] init];
    lineImgV.backgroundColor = [UIColor lightGrayColor];
    lineImgV.alpha = 0.6;
    [view addSubview:lineImgV];
    [lineImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view).offset(6);
        make.left.equalTo(view).offset(kNoteOffSet / 2.0 + 7.0);
        make.width.mas_equalTo(minLineWidth);
    }];
    
    lineImgV.KVOController = [FBKVOController controllerWithObserver:collectionView];
    [lineImgV.KVOController observe:collectionView keyPath:@"contentSize" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew block:^(id observer, id object, NSDictionary *change) {
        [lineImgV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(MAX(collectionView.contentSize.height - 20.0, 0));
            make.top.mas_equalTo(-1 * collectionView.contentOffset.y + 10.0);
        }];
    }];
    return collectionView;
}


//MARK: UICollection View DataSource & Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray *phaseDatas = self.dataSources[self.swipeView.currentPage];
    NSArray *notes = phaseDatas[section][@"NoteData"];
    return notes.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSArray *phaseDatas = self.dataSources[self.swipeView.currentPage];
    return phaseDatas.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JFNoteCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentify forIndexPath:indexPath];
    NSArray *phaseDatas = self.dataSources[self.swipeView.currentPage];
    NSArray *notes = phaseDatas[indexPath.section][@"NoteData"];
    [cell reloadNoteCellData:notes[indexPath.row]];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    //如果是头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        JFNoteHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HeadIdentify forIndexPath:indexPath];
        NSArray *phaseDatas = self.dataSources[self.swipeView.currentPage];
        NSDictionary *headInfo = phaseDatas[indexPath.section][@"HeadInfo"];
        [header reloadHeadData:headInfo];
        return header;
    }
    return JFNoteHeader.new;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    double width = (Screen_Width - 5 * 10.0 - kNoteOffSet) / 4.0;
    return Size(width, width);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return Size(Screen_Width, 45);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10 + kNoteOffSet, 10, 10);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [colView cellForItemAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor colorWithRed:242.0 / 255.0 green:242.0 / 255.0 blue:242.0 / 255.0 alpha:1.0]];
}

- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [colView cellForItemAtIndexPath:indexPath];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [cell setBackgroundColor:[UIColor colorWithHexString:@"ffd600"]];
    });
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

@end
